{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T05:46:19.670631Z",
     "start_time": "2025-06-26T05:46:19.653801Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#6.2 填充和步幅\n",
    "import torch\n",
    "from torch import nn\n"
   ],
   "id": "41ab83d26f0c7e9b",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T05:46:19.717414Z",
     "start_time": "2025-06-26T05:46:19.703920Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def comp_conv2d(conv2d, X):\n",
    "\tX = X.reshape((1,1)+X.shape) #添加批量和通道\n",
    "\tY = conv2d(X)\n",
    "\treturn Y.reshape(Y.shape[2:])# 去掉批量和通道"
   ],
   "id": "80229d547cb70302",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T05:46:19.747591Z",
     "start_time": "2025-06-26T05:46:19.732305Z"
    }
   },
   "cell_type": "code",
   "source": [
    "conv2d = nn.Conv2d(1,1,kernel_size=3,padding=1)\n",
    "X = torch.randn(size=(8,8))\n",
    "comp_conv2d(conv2d, X).shape"
   ],
   "id": "659fa9efddfae61e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([8, 8])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T05:47:44.271946Z",
     "start_time": "2025-06-26T05:47:44.260430Z"
    }
   },
   "cell_type": "code",
   "source": [
    "conv2d = nn.Conv2d(1, 1, kernel_size=(5, 3), padding=(2, 1))\n",
    "comp_conv2d(conv2d, X).shape"
   ],
   "id": "5a4b4f160b24a8e8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([8, 8])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T05:48:29.117522Z",
     "start_time": "2025-06-26T05:48:29.103505Z"
    }
   },
   "cell_type": "code",
   "source": [
    "conv2d = nn.Conv2d(1, 1, kernel_size=3, padding=1, stride=2)\n",
    "comp_conv2d(conv2d, X).shape"
   ],
   "id": "b06244972512f827",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 4])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T05:48:40.165004Z",
     "start_time": "2025-06-26T05:48:40.154345Z"
    }
   },
   "cell_type": "code",
   "source": [
    "conv2d = nn.Conv2d(1, 1, kernel_size=(3, 5), padding=(0, 1), stride=(3, 4))\n",
    "comp_conv2d(conv2d, X).shape"
   ],
   "id": "59b95bf77332c170",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 2])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "50349c64ce09e08f"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
